package panel.mainPanel
{
	import assists.TextFieldUtils;
	import assists.log.Log;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TransformGestureEvent;
	import flash.text.TextField;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	public class MainNewsBox extends Sprite
	{
		private var pointList:VerticalPointGroupList;
		private var textList:TextGroupList;
		
		private var _stage:Stage;
		private var _width:int = 0;
		private var _height:int = 0;
		
		private var currentPage:int = 0;
		private var dataList:Array;
		
		private var _onClick:Function;
		private var showDetail:Function;
		
		private var _dataList:Array;
		
		private var titleText:TextField;
		
		public function MainNewsBox(stage:Stage,wid:int,hei:int)
		{
			_stage = stage;
			
			_width = wid;
			_height = hei;
			
			dataList = [];
			
			init();
		}
		
		public function init():void
		{
			var back:Shape = new Shape();
			back.graphics.beginFill(0x000000,.3);
			back.graphics.drawRoundRect(0,0,_width,_height,10,10);
			back.graphics.endFill();
			this.addChild(back);
			back.x = 0;
			back.y = 0;
			
			var gray:Shape = new Shape();
			gray.graphics.beginFill(0x000000,0);
			gray.graphics.drawRoundRect(0,0,_width,_height,5,5);
			gray.graphics.endFill();
			this.addChild(gray);
			this.mask = gray;
			
			pointList = new VerticalPointGroupList(_width * .02);
			pointList.y = _width * .1;
			pointList.x = _width * .95;
			this.addChild(pointList);
			
			titleText = new TextField;
			titleText.multiline = false;
			titleText.wordWrap = false;
			titleText.mouseEnabled = false;
			
			this.addChild(titleText);
		}
		
		public function set title(str:String):void
		{
			TextFieldUtils.setHtmlText(titleText,str,true,true);
			
			titleText.width = titleText.textWidth + 5;
			titleText.height = titleText.textHeight + 5;
			
			titleText.x = (_width - titleText.width) / 2;
			titleText.y = _height * 0.01;
		}
		
		public function changeData(dataList:Array):void
		{
			_dataList = dataList;
			pointList.createPoint(dataList);
			
			createNews();
			
			reposition();
			addEvent();
		}
		
		private function createNews():void
		{
			if(textList == null){
				textList = new TextGroupList(_stage,_width * .8,_height);
				textList.clickAndShowDetail = showDetail;
				this.addChild(textList);
				textList.x = _width * .01;
				textList.y = _height * 0.02+ titleText.height;
			}
			
			textList.createText(_dataList);
		}
		
		private function reposition():void
		{
			pointList.changeData(currentPage);
			textList.changeData(currentPage);
		}
		
		private function addEvent():void
		{
			if(Multitouch.supportedGestures){
				Multitouch.inputMode = MultitouchInputMode.GESTURE;
				this.addEventListener(TransformGestureEvent.GESTURE_SWIPE,swipeHandler);
			}
			
			if(!this.hasEventListener(MouseEvent.CLICK)){
				this.addEventListener(MouseEvent.CLICK,onClickHandler);
			}
		}
		
		protected function onClickHandler(event:MouseEvent):void
		{
			if(_onClick is Function){
				_onClick(currentPage);
			}
		}
		
		private function removeEnent():void
		{
			if(this.hasEventListener(TransformGestureEvent.GESTURE_SWIPE)){
				this.addEventListener(TransformGestureEvent.GESTURE_SWIPE,swipeHandler);
			}
			
			if(this.hasEventListener(MouseEvent.CLICK)){
				this.removeEventListener(MouseEvent.CLICK,onClickHandler);
			}
		}
		
		public function clearText():void
		{
			if(textList){
				textList.clear();
			}
		}
		
		public function clear():void
		{
			clearText();
			
			removeEnent();
		}
		
		protected function swipeHandler(e:TransformGestureEvent):void
		{
			if(e.offsetY < 0){
				nextPage();
			}else{
				formerPage();
			}
		}
		
		private function formerPage():void
		{
			if(currentPage > 0){
				currentPage = currentPage - 1;
				reposition();
			}else{
				return;
			}
		}
		
		private function nextPage():void
		{
			if(currentPage < _dataList.length - 1){
				currentPage = currentPage + 1;
				reposition();
			}else{
				return;
			}
		}
		
		public function set clickCallBack(func:Function):void
		{
			_onClick = func;
		}
		
		public function set onShowDetail(func:Function):void
		{
			showDetail = func
		}
	}
}